from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
from .models import Testmodel,Kaoshi as Kaoshimodel
import json
# Create your views here.
class TestView(View):
    def get(self,req):
        print(req.GET)
        pagesize=int(req.GET.get("size"))
        pagenum=int(req.GET.get("page"))
        start=(pagenum-1)*pagesize
        end=start+pagesize
        if req.GET.get('sjname'):
            data = Testmodel.objects.filter(sjname__contains=req.GET.get('sjname'))[start:end].values()
            total = len(Testmodel.objects.filter(sjname__contains=req.GET.get('sjname')))
        elif req.GET.get('id'):
            data={}
            datas = Testmodel.objects.filter(id=req.GET.get('id')).values()
            z=eval(datas[0]['question'])
            data['que']=z
            data['id']=datas[0]['id']
            data['name'] = datas[0]['name']
            data['sjname'] = datas[0]['sjname']
            data['tscore'] = datas[0]['tscore']
            data['createtime'] = datas[0]['createtime']
            data=[data]
            total = len(Testmodel.objects.filter(id=req.GET.get('id')).values())
        else:
            data=Testmodel.objects.values()[start:end]
            total=len(Testmodel.objects.values())
        return JsonResponse({"message":"获取成功","code":200,"data":list(data),"total":total})
    def post(self,req):
        d=json.loads(req.body)
        u=Testmodel.objects.create(**d)
        return JsonResponse({"message":"获取成功","code":200})
class KaoshiView(View):
    def get(self, req):
        pagesize = int(req.GET.get("size"))
        pagenum = int(req.GET.get("page"))
        start = (pagenum - 1) * pagesize
        end = start + pagesize
        if req.GET.get('name'):
            data = Kaoshimodel.objects.filter(name__contains=req.GET.get('name'))[start:end].values()
            total = len(Kaoshimodel.objects.filter(name__contains=req.GET.get('name')))
        elif req.GET.get('id'):
            data={}
            datas = Kaoshimodel.objects.filter(id=req.GET.get('id')).values()
            z=eval(datas[0]['question'])
            data['que']=z
            data['id']=datas[0]['id']
            data['name'] = datas[0]['name']
            data['tscore'] = datas[0]['tscore']
            data['testid_id'] = datas[0]['testid_id']
            data=[data]
            total = 1
        else:
            data = Kaoshimodel.objects.values()[start:end]
            total = len(Kaoshimodel.objects.values())
        L=[]
        a=Kaoshimodel.objects.all()
        for i in a:
            dict = {}
            dict['name']=i.testid.name
            dict['sjname'] = i.testid.sjname
            dict['question'] = i.testid.question
            dict['tscore'] = i.testid.tscore
            dict['createtime'] = i.testid.createtime
            L.append(dict)

        for i in range(len(data)):
            data[i]['testinfo']=L[i]


        return JsonResponse({"message": "获取成功", "code": 200, "data": list(data), "total": total})

    def post(self, req):
        d = json.loads(req.body)
        u = Kaoshimodel.objects.create(name=d['name'],question=d['question'],testid_id=d['testid'],classbj=d['classbj'],phone=d['phone'])
        return JsonResponse({"message": "获取成功", "code": 200})
    def put(self,req,id):
        print(req.body)
        d=json.loads(req.body)
        Kaoshimodel.objects.filter(id=id).update(tscore=d['tscore'])
        return JsonResponse({"message":"修改成功","code":200})
